package br.com.site.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 
 * Classe utilitária de envio de e-mails.
 *  
 */

public class SendMail {

    private String mailHost;

    private String mailUser;

    private String messageText;

    private String messageSubject;

    private String nameFrom;

    private String addressFrom;

    private ArrayList addressTo = new ArrayList();

    private ArrayList addressCcTo = new ArrayList();

    private ArrayList addressBccTo = new ArrayList();

    private ArrayList addressReplyTo = new ArrayList();

    private String mailPort;
    
    private String password;

    //private static Logger logger = Logger.getLogger(SimpleEMailSender.class);

    public SendMail() {

    }

    // Métodos Get's e Set's

    public void addBCC(String nome, String email) throws Exception {
        Address address = new InternetAddress(email, nome);
        this.addressBccTo.add(address);
    }

    public void addCC(String nome, String email) throws Exception {
        Address address = new InternetAddress(email, nome);
        this.addressCcTo.add(address);
    }

    public void addTo(String nome, String email) throws Exception {
        Address address = new InternetAddress(email, nome);
        this.addressTo.add(address);
    }

    public void addReplyTo(String nome, String email) throws Exception {
        Address address = new InternetAddress(email, nome);
        this.addressReplyTo.add(address);
    }

    public String getMailHost() {
        return this.mailHost;
    }

    /**
     * @param mailHost
     *            The mailHost to set.
     */
    public void setMailHost(String mailHost) {
        this.mailHost = mailHost;
    }

    /**
     * @return Returns the mailUser.
     */
    public String getMailUser() {
        return this.mailUser;
    }

    /**
     * @param mailUser
     *            The mailUser to set.
     */
    public void setMailUser(String mailUser) {
        this.mailUser = mailUser;
    }

    /**
     * @return Returns the messageSubject.
     */
    public String getMessageSubject() {
        return this.messageSubject;
    }

    /**
     * @param messageSubject
     *            The messageSubject to set.
     */
    public void setMessageSubject(String messageSubject) {
        this.messageSubject = messageSubject;
    }

    /**
     * @return Returns the messageText.
     */
    public String getMessageText() {
        return this.messageText;
    }

    /**
     * @param messageText
     *            The messageText to set.
     */
    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    /**
     * @return Returns the nameFrom.
     */
    public String getNameFrom() {
        return this.nameFrom;
    }

    /**
     * @param nameFrom
     *            The nameFrom to set.
     */
    public void setNameFrom(String nameFrom) {
        this.nameFrom = nameFrom;
    }

    /**
     * @return Returns the addressFrom.
     */
    public String getAddressFrom() {
        return this.addressFrom;
    }

    /**
     * @param addressFrom
     *            The addressFrom to set.
     */
    public void setAddressFrom(String addressFrom) {
        this.addressFrom = addressFrom;
    }
    
    // Fim dos métodos Get's e Set's

    /**
     * 
     * Envia o e-mail.
     * 
     * 
     * 
     * @throws MessagingException
     * 
     * @throws UnsupportedEncodingException
     *  
     */

    public final void send() throws MessagingException,
            UnsupportedEncodingException {

        Session mailSession = getMailSession();
        

        // Criando a mensagem
        MimeMessage msg = new MimeMessage(mailSession);

        // Inserindo os dados na mensagem
        msg.setText(messageText);
        msg.setSubject(messageSubject);
        
        //Dados do remetente

        Address fromAddress = new InternetAddress(addressFrom, nameFrom);
        msg.setFrom(fromAddress);

        //Dados do destinatário
        Iterator emails = addressTo.iterator();
        while (emails.hasNext()) {
            Address email = (Address) emails.next();
            msg.addRecipient(Message.RecipientType.TO, email);
        }

        emails = addressCcTo.iterator();
        while (emails.hasNext()) {
            Address email = (Address) emails.next();
            msg.addRecipient(Message.RecipientType.CC, email);
        }

        emails = addressBccTo.iterator();
        while (emails.hasNext()) {
            Address email = (Address) emails.next();
            msg.addRecipient(Message.RecipientType.BCC, email);
        }

        if (addressReplyTo.size() != 0) {
            Address[] type = {};
            msg.setReplyTo((Address[]) addressReplyTo.toArray(type));
        }

        //Enviando a mensagem

        //logger.debug("Enviando e-mail para " + toAddress + "...");
        
        Transport.send(msg);

        //logger.debug("E-mail para " + toAddress + " enviado.");

    }

    /**
     * 
     * Retorna a sessão de e-mail.
     * 
     * 
     * 
     * @return sessão de e-mail.
     *  
     */

    private Session getMailSession() {

        Session mailSession;

        // Throw exception if mail host or user are not set

        if (mailHost == null || mailUser == null) {
            throw new IllegalStateException(
                    "Parametros mailHost e mailUser não foram configurados.");
        }

        // Create mail session if it doesn't exist
        Properties p = new Properties();

        p.setProperty("mail.host", this.mailHost);
        p.setProperty("mail.user", this.mailUser);
        p.setProperty("mail.passwd", this.password);
        
        //p.setProperty("mail.smtp.auth", "false");
        p.put("mail.smtp.auth", "true");
        
        if (this.mailPort != null) {
            p.put("mail.smtp.port", this.mailPort);
        }

        //mailSession = Session.getDefaultInstance(p, null);
        mailSession = Session.getDefaultInstance(p, new javax.mail.Authenticator() {  
        	  
            protected PasswordAuthentication getPasswordAuthentication() {  
                return new PasswordAuthentication(getMailUser(),getPassword());  
            }  
        });  

        return mailSession;

    }

    /**
     * @return Returns the mailPort.
     */
    public String getMailPort() {
        return this.mailPort;
    }

    /**
     * @param mailPort The mailPort to set.
     */
    public void setMailPort(String mailPort) {
        this.mailPort = mailPort;
    }

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
    
    

}

